package ro.isdc.training.ejb3.misc.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * Business Entities have identities and are always mapped to tables.
 * 
 * @author Andrei Chirila
 */
@MappedSuperclass
@EntityListeners(BusinessEntityAuditListener.class)
public abstract class BusinessEntity extends DataObject {
		
	/**
   * Generated Serial Version UID 
   */
  private static final long serialVersionUID = -2653622712117020793L;
  
  /**
	 * Unique internal entity identifier (primary key for non-version 
	 * entities).
	 */
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	protected long entityId = -1;
	
	/**
	 * Version column for enabling optimistic locking strategy.
	 */
	@Version
	protected int versionNo = 0;
	
	/**
	 * The creation timestamp. The type is temporal : timestamp.
	 * If not specified, timestamp is the temporal type by default
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(updatable=false)
	protected Date creationTimestamp;
	
	/**
	 * The last update timestamp.
	 */
	//by default : TIMESTAMP
	protected Date lastUpdateTimestamp;
	
	/**
	 * @return the entityId
	 */
	public long getEntityId() {
		return entityId;
	}

	/**
	 * @param entityId the entityId to set
	 */
	public void setEntityId(final long entityId) {
		this.entityId = entityId;
	}

	/**
   * @return the versionNo
   */
  public int getVersionNo() {
    return versionNo;
  }

  /**
   * @param versionNo the versionNo to set
   */
  public void setVersionNo(final int versionNo) {
    this.versionNo = versionNo;
  }
  
  /**
	 * @return the creationTimestamp
	 */
	public Date getCreationTimestamp() {
		return creationTimestamp;
	}

	/**
	 * @param creationTimestamp the creationTimestamp to set
	 */
	public void setCreationTimestamp(final Date creationTimestamp) {
		this.creationTimestamp = creationTimestamp;
	}

	/**
	 * @return the lastUpdateTimestamp
	 */
	public Date getLastUpdateTimestamp() {
		return lastUpdateTimestamp;
	}

	/**
	 * @param lastUpdateTimestamp the lastUpdateTimestamp to set
	 */
	public void setLastUpdateTimestamp(final Date lastUpdateTimestamp) {
		this.lastUpdateTimestamp = lastUpdateTimestamp;
	}

	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(final Object object) {
		if (object == this) {
			return true;
		}
		if (!(object instanceof BusinessEntity)) {
			return false;
		}
		BusinessEntity rhs = (BusinessEntity) object;
		return new EqualsBuilder().append(this.entityId,
				rhs.entityId).isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(-2103341447, 1374325971).append(
				this.entityId).toHashCode();
	}
	
}

